const dd = new Date();

import { getBill } from '../api/index';



const state = {
    month_pay_price: 0,
    month_get_price: 0,
    month: localStorage.getItem('myDate') ? JSON.parse(localStorage.getItem('myDate')).myMonth : (dd.getMonth() + 1) + '',
    year: localStorage.getItem('myDate') ? JSON.parse(localStorage.getItem('myDate')).myYear : dd.getFullYear() + '',
    /*MonthlyCommit组件日期信息*/
    monthlyCommit: [
        // {
        //     date: '',
        //     bills: [
        //         // {
        //         //     describeBill:'',
        //         //     price:Number,
        //         //     priceType:Boolean,
        //         //     type:Number,
        //         // }
        //     ]
        // }
    ],

};

const actions = {
    async getInfo(context, value) {
        if (!sessionStorage.getItem('loginToken')) {
            alert('未包含有效token,请合法获取数据');
        } else {
            const token = sessionStorage.getItem('loginToken');
            const result = await getBill(value, token);
            if (result.code === 200) {
                context.commit('BILLINFO', result);
            } else {
                alert('数据读取失败');
            }
        }
    }
};

const mutations = {
    UPDATEMONTH(state, value) {
        state.month = value;
    },
    UPDATEYEAR(state, value) {
        state.year = value;
    },
    BILLINFO(state, result) {
        console.log(result.result);
        state.month_pay_price = result.result.month_pay_money;
        state.month_get_price = result.result.month_get_money;
        state.monthlyCommit.splice(0);
        for (let i = 0; i < result.result.data.length; i++) {
            state.monthlyCommit.push(result.result.data[i]);
            console.log(result.result.data[i]);
        }
    }
};

const getters = {
    toFixed_month_pay_price() {
        return state.month_pay_price.toFixed(2);
    },
    toFixed_month_get_price() {
        return state.month_get_price.toFixed(2);
    },
    hasData() {
        return state.monthlyCommit.length > 0;
    }
};

export default {
    namespaced: true,
    state,
    actions,
    mutations,
    getters,
}